package com.xuxin.baby.presenter;

import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.ChildEatingRecordContract;
import com.xuxin.baby.model.ChildEatingRecordModel;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020/10/22 0022
 * 记喂养
 */
public class ChildEatingRecordPresenter implements ChildEatingRecordContract.Presenter {
    private  final ChildEatingRecordModel model;
    private final  ChildEatingRecordContract.View view;

    public ChildEatingRecordPresenter(ChildEatingRecordContract.View view) {
        this.view = view;
        model = new ChildEatingRecordModel();
    }

    @Override
    public void recordBreast(String tenantId, String token, String childId, String eventDate, float feedTimes) {
        if (null != model) {
            try {
                JSONObject object = new JSONObject();
                object.put("childId", childId);
                object.put("eventDate", eventDate);
                object.put("feedTimes", feedTimes);
                RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
                model.record(this, tenantId, token, requestBody);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void recordBottle(String tenantId, String token, String childId, String eventDate, float feedBreastAmt) {
        if (null != model) {
            try {
                JSONObject object = new JSONObject();
                object.put("childId", childId);
                object.put("eventDate", eventDate);
                object.put("feedBreastAmt", feedBreastAmt);
                RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
                model.record(this, tenantId, token, requestBody);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void recordDried(String tenantId, String token, String childId, String eventDate, float feedPowderedAmt) {
        if (null != model) {
            try {
                JSONObject object = new JSONObject();
                object.put("childId", childId);
                object.put("eventDate", eventDate);
                object.put("feedPowderedAmt", feedPowderedAmt);
                RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
                model.record(this, tenantId, token, requestBody);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void recordHelp(String tenantId, String token, String childId, String eventDate, String eventContent, String pic1) {
        if (null != model) {
            try {
                JSONObject object = new JSONObject();
                object.put("childId", childId);
                object.put("eventDate", eventDate);
                object.put("eventContent", eventContent);
                object.put("pic1", pic1);
                RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
                model.record(this, tenantId, token, requestBody);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        if (null != view) {
            view.onSuccess(baseBean);
        }
    }

    /**
     * 以下内容复用(用于请求完成后的操作)
     */
    @Override
    public void onError(BaseBean baseBean) {
        if (null != view) {
            view.onError(baseBean);
        }
    }

    @Override
    public void onComplete() {
        if (null != view) {
            view.onComplete();
        }
    }
}